﻿using System;


using System.Text;
using System.Net;
using System.IO;

namespace IAM.Interface.Common.Providers
{
    public class SAPProvider : IProvider
    {
        #region 伪代码
        #region 构造函数
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="url">目的地址</param>
        /// <param name="xml">xml信息</param>
        public SAPProvider(string url, string sendstring)
        {
            _url = url;
            _sendstring = sendstring;
        }
        #endregion
        #region 依据发出的请求返回响应的xml字符串
        public override string GetResponseStr()
        {
            string str = "";
            try
            {
                //获取请求xml

                byte[] bs = Encoding.ASCII.GetBytes("xml=" + _sendstring);
                HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(_url);
                httpRequest.Method = "post";

                httpRequest.ContentType = "text/xml";
                httpRequest.ContentLength = bs.Length;

                using (Stream reqStream = httpRequest.GetRequestStream())
                {
                    reqStream.Write(bs, 0, bs.Length);
                    reqStream.Flush();
                }

                //httpRequest.UserAgent = @"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.40607; .NET CLR 1.1.4322)";
                HttpWebResponse httpResponse = (HttpWebResponse)httpRequest.GetResponse();

                if (httpRequest.HaveResponse)
                {
                    StreamReader sr;

                    sr = new StreamReader(httpResponse.GetResponseStream(), Encoding.Default);
                    str = sr.ReadToEnd();

                    sr.Close();
                    httpResponse.Close();
                }
            }
            catch (Exception ex)
            {
                throw (new Exception(ex.Message));
            }
            return str;
        }
        #endregion
        #endregion
    }
}
